/*
 * JFileBrowser.java
 *
 * Created on 10 de julio de 2008, 16:11
 */
package com.cartiec.jrenamer;

import java.io.File;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Tonny
 */
public class JFileBrowser extends javax.swing.JPanel {

    public static String MOUSE_CLICKED = "MOUSE_CLICKED";
    DefaultMutableTreeNode top = null;
    private File selectetFile = new File("");

    public File getSelectetFile() {
        return selectetFile;
    }

    /** Creates new form JFileBrowser */
    public JFileBrowser() {
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/cartiec/jrenamer/JFileBrowser"); // NOI18N
        top = new DefaultMutableTreeNode(bundle.getString("raiz"));
        initComponents();
        tree.setCellRenderer(new FilesTreeCellRenderer());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        treeView = new javax.swing.JScrollPane();
        tree = new JTree(top);

        setLayout(new java.awt.BorderLayout());

        tree.setToggleClickCount(1);
        tree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeMouseClicked(evt);
            }
        });
        tree.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
                treeTreeCollapsed(evt);
            }
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                treeTreeExpanded(evt);
            }
        });
        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });
        treeView.setViewportView(tree);

        add(treeView, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void treeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeMouseClicked
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    Object o = node.getUserObject();
    selectetFile = (o instanceof File) ? (File) o : null;
    firePropertyChange(MOUSE_CLICKED, null, selectetFile);
    checkNode((DefaultMutableTreeNode) node);
}//GEN-LAST:event_treeMouseClicked

private void treeTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_treeTreeExpanded
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    for (int i = 0; i < node.getChildCount(); i++) {
        checkNode((DefaultMutableTreeNode) node.getChildAt(i));
    }
}//GEN-LAST:event_treeTreeExpanded

private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    checkNode(node);
}//GEN-LAST:event_treeValueChanged

private void treeTreeCollapsed(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_treeTreeCollapsed
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    checkNode(node);
}//GEN-LAST:event_treeTreeCollapsed

    private void checkNode(DefaultMutableTreeNode node) {
        DefaultMutableTreeNode rootFile = null;
        if (node == null) {
            return;
        }

        Object nodeInfo = node.getUserObject();
        if (nodeInfo instanceof String) {
            File[] roots = File.listRoots();
            if (top.getChildCount() == 0) {
                for (int i = 0; i < roots.length; ++i) {
                    rootFile = new DefaultMutableTreeNode(roots[i]);
                    if (roots[i].isDirectory()) {
                        top.add(rootFile);
                    }
                }
            }
        }
        
        if((nodeInfo instanceof File) && (((File) nodeInfo).isDirectory())){
            File files[] = ((File) nodeInfo).listFiles();
            if (files == null) {
                return;
            }
            if(files.length != node.getChildCount()){
                node.removeAllChildren();
            }
        }
                    
        if ((node.getChildCount() == 0) && (nodeInfo instanceof File) && (((File) nodeInfo).isDirectory())) {
            File files[] = ((File) nodeInfo).listFiles();
            if (files == null) {
                return;
            }
            for (File file : files) {
                rootFile = new DefaultMutableTreeNode(file);
                if (file.isDirectory()) {
                    node.add(rootFile);
                }
            }
        }     
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree tree;
    private javax.swing.JScrollPane treeView;
    // End of variables declaration//GEN-END:variables

}
